{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": "    annotation_id  annotator                   created_at     id  \\\n0            6691          7  2023-11-24T02:19:34.544288Z  15156   \n1            6692          7  2023-11-24T02:27:04.302629Z  15157   \n2            6694          7  2023-11-24T02:29:03.698101Z  15158   \n3            6695          7  2023-11-24T02:31:18.066502Z  15159   \n4            6696          7  2023-11-24T02:33:23.262267Z  15160   \n..            ...        ...                          ...    ...   \n72           6880          7  2023-11-24T07:19:17.073584Z  15229   \n73           6882          7  2023-11-24T07:20:23.725693Z  15230   \n74           6883          7  2023-11-24T07:20:46.743673Z  15231   \n75           6891          7  2023-11-24T07:32:49.530439Z  15232   \n76           6892          7  2023-11-24T07:38:30.941820Z  15233   \n\n                                               labels  lead_time  \\\n0   [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...     77.895   \n1   [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...    285.210   \n2   [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...    106.300   \n3   [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...    126.491   \n4   [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...    123.976   \n..                                                ...        ...   \n72  [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...     55.538   \n73  [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...     65.593   \n74  [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...     19.110   \n75  [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...     76.586   \n76  [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...    327.834   \n\n                                             messages  \\\n0   [{\"role\":\"货主\",\"content\":\"1、装货地：南京浦口区@#_#@_2、目的...   \n1   [{\"role\":\"货主\",\"content\":\"你现在帮我发一个 晚上9点~10点装货。@...   \n2   [{\"role\":\"货主\",\"content\":\"明天下午装，山东潍坊寿光市欣禾食品有限公司...   \n3   [{\"role\":\"货主\",\"content\":\"装货点：江苏南京江宁区禄口镇@#_#@_卸...   \n4   [{\"role\":\"货主\",\"content\":\"1、装货地四川资阳@#_#@_2、目的地：...   \n..                                                ...   \n72  [{\"role\":\"货主\",\"content\":\"寿光正方到日照五莲火车站附近 2吨鸭冻品 ...   \n73  [{\"role\":\"货主\",\"content\":\"发货地：乌鲁木齐头屯河@#_#@_2、目的...   \n74  [{\"role\":\"货主\",\"content\":\"始发地： 南通海安@#_#@_目的地： 上...   \n75  [{\"role\":\"货主\",\"content\":\"发货地址：云霄闽正@#_#@_收货地址：南...   \n76  [{\"role\":\"货主\",\"content\":\"一装两卸 柳州120，南宁104」@#_#...   \n\n                     updated_at  \n0   2023-11-24T02:19:34.544354Z  \n1   2023-11-24T02:27:04.302678Z  \n2   2023-11-24T02:29:03.698155Z  \n3   2023-11-24T02:31:18.066584Z  \n4   2023-11-24T02:33:23.262319Z  \n..                          ...  \n72  2023-11-24T07:19:17.073634Z  \n73  2023-11-24T07:20:23.725751Z  \n74  2023-11-24T07:20:46.743724Z  \n75  2023-11-24T07:32:49.530488Z  \n76  2023-11-24T07:38:30.941888Z  \n\n[77 rows x 8 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>annotation_id</th>\n      <th>annotator</th>\n      <th>created_at</th>\n      <th>id</th>\n      <th>labels</th>\n      <th>lead_time</th>\n      <th>messages</th>\n      <th>updated_at</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>6691</td>\n      <td>7</td>\n      <td>2023-11-24T02:19:34.544288Z</td>\n      <td>15156</td>\n      <td>[{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...</td>\n      <td>77.895</td>\n      <td>[{\"role\":\"货主\",\"content\":\"1、装货地：南京浦口区@#_#@_2、目的...</td>\n      <td>2023-11-24T02:19:34.544354Z</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>6692</td>\n      <td>7</td>\n      <td>2023-11-24T02:27:04.302629Z</td>\n      <td>15157</td>\n      <td>[{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...</td>\n      <td>285.210</td>\n      <td>[{\"role\":\"货主\",\"content\":\"你现在帮我发一个 晚上9点~10点装货。@...</td>\n      <td>2023-11-24T02:27:04.302678Z</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>6694</td>\n      <td>7</td>\n      <td>2023-11-24T02:29:03.698101Z</td>\n      <td>15158</td>\n      <td>[{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...</td>\n      <td>106.300</td>\n      <td>[{\"role\":\"货主\",\"content\":\"明天下午装，山东潍坊寿光市欣禾食品有限公司...</td>\n      <td>2023-11-24T02:29:03.698155Z</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>6695</td>\n      <td>7</td>\n      <td>2023-11-24T02:31:18.066502Z</td>\n      <td>15159</td>\n      <td>[{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...</td>\n      <td>126.491</td>\n      <td>[{\"role\":\"货主\",\"content\":\"装货点：江苏南京江宁区禄口镇@#_#@_卸...</td>\n      <td>2023-11-24T02:31:18.066584Z</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>6696</td>\n      <td>7</td>\n      <td>2023-11-24T02:33:23.262267Z</td>\n      <td>15160</td>\n      <td>[{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...</td>\n      <td>123.976</td>\n      <td>[{\"role\":\"货主\",\"content\":\"1、装货地四川资阳@#_#@_2、目的地：...</td>\n      <td>2023-11-24T02:33:23.262319Z</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>72</th>\n      <td>6880</td>\n      <td>7</td>\n      <td>2023-11-24T07:19:17.073584Z</td>\n      <td>15229</td>\n      <td>[{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...</td>\n      <td>55.538</td>\n      <td>[{\"role\":\"货主\",\"content\":\"寿光正方到日照五莲火车站附近 2吨鸭冻品 ...</td>\n      <td>2023-11-24T07:19:17.073634Z</td>\n    </tr>\n    <tr>\n      <th>73</th>\n      <td>6882</td>\n      <td>7</td>\n      <td>2023-11-24T07:20:23.725693Z</td>\n      <td>15230</td>\n      <td>[{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...</td>\n      <td>65.593</td>\n      <td>[{\"role\":\"货主\",\"content\":\"发货地：乌鲁木齐头屯河@#_#@_2、目的...</td>\n      <td>2023-11-24T07:20:23.725751Z</td>\n    </tr>\n    <tr>\n      <th>74</th>\n      <td>6883</td>\n      <td>7</td>\n      <td>2023-11-24T07:20:46.743673Z</td>\n      <td>15231</td>\n      <td>[{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...</td>\n      <td>19.110</td>\n      <td>[{\"role\":\"货主\",\"content\":\"始发地： 南通海安@#_#@_目的地： 上...</td>\n      <td>2023-11-24T07:20:46.743724Z</td>\n    </tr>\n    <tr>\n      <th>75</th>\n      <td>6891</td>\n      <td>7</td>\n      <td>2023-11-24T07:32:49.530439Z</td>\n      <td>15232</td>\n      <td>[{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...</td>\n      <td>76.586</td>\n      <td>[{\"role\":\"货主\",\"content\":\"发货地址：云霄闽正@#_#@_收货地址：南...</td>\n      <td>2023-11-24T07:32:49.530488Z</td>\n    </tr>\n    <tr>\n      <th>76</th>\n      <td>6892</td>\n      <td>7</td>\n      <td>2023-11-24T07:38:30.941820Z</td>\n      <td>15233</td>\n      <td>[{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...</td>\n      <td>327.834</td>\n      <td>[{\"role\":\"货主\",\"content\":\"一装两卸 柳州120，南宁104」@#_#...</td>\n      <td>2023-11-24T07:38:30.941888Z</td>\n    </tr>\n  </tbody>\n</table>\n<p>77 rows × 8 columns</p>\n</div>"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "files = []\n",
    "for file in os.listdir('data_agent_after_biaozhu'):\n",
    "    real_path = os.path.join('data_agent_after_biaozhu',file)\n",
    "    if os.path.isdir(real_path):\n",
    "        continue\n",
    "    files.append(real_path)\n",
    "df = pd.read_csv(files[0])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "annotation_id                                                 6691\nannotator                                                        7\ncreated_at                             2023-11-24T02:19:34.544288Z\nid                                                           15156\nlabels           [{\"start\":\"1\",\"end\":\"1\",\"startOffset\":0,\"endOf...\nlead_time                                                   77.895\nmessages         [{\"role\":\"货主\",\"content\":\"1、装货地：南京浦口区@#_#@_2、目的...\nupdated_at                             2023-11-24T02:19:34.544354Z\nName: 0, dtype: object"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[0,:]"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "[{'start': '1',\n  'end': '1',\n  'startOffset': 0,\n  'endOffset': 5,\n  'text': '第一次思考',\n  'paragraphlabels': ['对话状态-信息收集阶段', '工具-发货信息提取']},\n {'start': '1',\n  'end': '1',\n  'startOffset': 7,\n  'endOffset': 12,\n  'text': '第二次思考',\n  'paragraphlabels': ['对话状态-可执行发货阶段', '工具-确认发货信息']},\n {'start': '4',\n  'end': '4',\n  'startOffset': 0,\n  'endOffset': 5,\n  'text': '第一次思考',\n  'paragraphlabels': ['对话状态-可执行发货阶段', '工具-执行发货']}]"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "json.loads(df.iloc[0,:][\"labels\"])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "[{'role': '货主',\n  'content': '1、装货地：南京浦口区@#_#@_2、目的地：上海青浦华徐公路普洛斯、浦东浦三路领鲜仓库@#_#@_3、货名：预制食品@#_#@_4、重量：10@#_#@_5、体积：26@#_#@_6、需求车型:6.8@#_#@_7、装货时间 ：11月20号13点前@#_#@_8、到货时间：11月20日21点前卸货@#_#@_9、发货人：王总@#_#@_10、收货人：王总@#_#@_11、预算费用：1400@#_#@_要求：当天卸货，冷藏温度0-6℃'},\n {'role': 'Agent', 'content': '第一次思考  第二次思考  第三次思考'},\n {'role': '客服',\n  'content': '好的老板，跟你确认一下。装货地是南京浦口区，目的地是上海青浦华徐公路普洛斯、浦东浦三路领鲜仓库，货物是预制食品，重10吨，体积26方，需要车长6.8米，11月20号13点前装货，11月20日21点前卸货，预算1400元，当天卸货，冷藏温度0-6℃'},\n {'role': '货主', 'content': '发货可以进行了。'},\n {'role': 'Agent', 'content': '第一次思考  第二次思考  第三次思考'}]"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "json.loads(df.iloc[0,:][\"messages\"])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "422"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datas = []\n",
    "\n",
    "\n",
    "for index,row in df.iterrows():\n",
    "    messages = json.loads(row['messages'])\n",
    "    labels = json.loads(row['labels'])\n",
    "    labels.sort(key=lambda x: int(x[\"start\"]) + x[\"startOffset\"]/100)\n",
    "\n",
    "    for index, label_dict in enumerate(labels):\n",
    "        info = {}\n",
    "        if \"对话状态-可执行发货阶段\" not in label_dict[\"paragraphlabels\"]:\n",
    "            info['dialogue_status'] = \"对话状态-信息收集阶段\"\n",
    "            label_dict[\"paragraphlabels\"].remove(\"对话状态-信息收集阶段\")\n",
    "        else:\n",
    "            info['dialogue_status'] = \"对话状态-可执行发货阶段\"\n",
    "            label_dict[\"paragraphlabels\"].remove(\"对话状态-可执行发货阶段\")\n",
    "\n",
    "        if len(label_dict[\"paragraphlabels\"]) != 1:\n",
    "            raise ValueError(\"标注标签数量不对\")\n",
    "        info[\"agentAction\"] = label_dict[\"paragraphlabels\"][0]\n",
    "\n",
    "        dialogs = []\n",
    "        for sen_dict in messages[:int(label_dict['start'])-1]:\n",
    "            if sen_dict['role'] == \"Agent\":\n",
    "                continue\n",
    "            dialogs.append(sen_dict['role'] +\":\"+sen_dict[\"content\"])\n",
    "\n",
    "        info[\"History\"] = \"\\n\".join(dialogs)\n",
    "        info[\"agentTurn\"] = int(label_dict[\"start\"])\n",
    "\n",
    "        observation = \"\"\n",
    "        if info[\"agentAction\"] == \"工具-发货信息提取\":\n",
    "            observation = \"已完成发货信息提取工具调用。请继续思考\"\n",
    "        else:\n",
    "            if info[\"agentTurn\"] + 1 < len(messages):\n",
    "                observation = messages[info[\"agentTurn\"] + 1]['role'] +\":\"+messages[info[\"agentTurn\"] + 1][\"content\"]\n",
    "        info[\"agentObservation\"] = observation\n",
    "        info[\"agentAction\"] = info[\"agentAction\"].replace(\"工具-\",\"\")\n",
    "\n",
    "\n",
    "\n",
    "        history_thoughts = [i for i in datas if i[\"agentTurn\"] == info[\"agentTurn\"]]\n",
    "\n",
    "        history_thoughts_str = \"\"\n",
    "        for index, h in enumerate(history_thoughts):\n",
    "            action = h['agentAction']\n",
    "            agentTurn = h[\"agentTurn\"]\n",
    "            observation = h[\"agentObservation\"]\n",
    "            dialogue_status  = h['dialogue_status']\n",
    "\n",
    "            temp_str = \"\"\n",
    "            if index != 0:\n",
    "                temp_str += \"对话状态:\" + dialogue_status + \"\\n\"\n",
    "            temp_str += \"Action:\" + action + '\\n'\n",
    "            temp_str += \"Observation:\" + observation +\"\\n\"\n",
    "            temp_str += \"\\n\"\n",
    "            history_thoughts_str += temp_str\n",
    "        info[\"agent_scratchpad\"] = history_thoughts_str\n",
    "        info['user_input'] = messages[info[\"agentTurn\"] - 1][\"content\"]\n",
    "\n",
    "        datas.append(info)\n",
    "len(datas)\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [],
   "source": [
    "prompt_str = '''\n",
    "角色(Role):\n",
    "    你是一个智能发货助手，专门负责处理和提取用户提供的发货信息，确保所有必要的数据都被准确捕获和利用。\n",
    "\n",
    "目标与关键结果(Objectives & Key Results):\n",
    "    目标(Objective): 准确地从用户输入中提取发货信息，并根据这些信息引导用户完成发货流程。\n",
    "\n",
    "关键结果(Key Results):\n",
    "    1. 成功识别并提取用户输入中的车长、收货地、发货地和车型信息。\n",
    "    2. 若用户输入信息不完整，主动询问缺失的细节，如未提及的发货地或收货地。\n",
    "    3. 确保信息准确无误，为顺利完成发货流程提供必要的支持。\n",
    "\n",
    "工具(Actions)：\n",
    "  智能发货助手可以使用的工具如下：\n",
    "    - 发货信息提取:当用户输入文本里面已经提供了一些发货信息，如车长、收货地、发货地和车型时。需要调用这个工具，提取发货信息\n",
    "    - 询问发货信息:当发货助手仍然缺失必要的发货信息时，需要调用这个工具来提示用户输入发货信息。\n",
    "    - 执行发货:当所有必要的发货信息已经被提取和确认后，执行发货流程。\n",
    "    - 取消发货:当用户想要取消发货是，调用这个工具\n",
    "    - 确认发货信息:在执行发货前，和用户确认所有的发货信息是否正确。\n",
    "\n",
    "对话阶段(Status):\n",
    "    - 信息收集阶段：仍然缺失必要的发货信息\n",
    "    - 可执行发货阶段：已经完成所有必要的发货信息的收集\n",
    "\n",
    "步骤(Steps):\n",
    "    1. 智能发货助手必须按照以下格式进行思考和反馈：\n",
    "        - Action: 工具名称应该是[发货信息提取，询问发货信息，执行发货,取消发货,确认发货信息]中的一个。\n",
    "        - Observation: 动作执行以后的反馈。\n",
    "    2. 根据上一步的Observation结果，如果有需要进行下一轮的思考和行动，直到发货流程顺利完成。\n",
    "\n",
    "开始！！\n",
    "历史对话：\n",
    "{chat_history}\n",
    "\n",
    "对话阶段：{dialogue_status}\n",
    "用户：{user_input}\n",
    "{agent_scratchpad}\n",
    "\n",
    "'''\n",
    "\n",
    "train_data  = []\n",
    "for info in datas:\n",
    "    chat_history = info[\"History\"]\n",
    "    dialogue_status = info[\"dialogue_status\"]\n",
    "    user_input = info[\"user_input\"]\n",
    "    agent_scratchpad = info['agent_scratchpad']\n",
    "\n",
    "    resultAction = info[\"agentAction\"]\n",
    "    resultObservation = info[\"agentObservation\"]\n",
    "\n",
    "\n",
    "    prompt_input = prompt_str.format(chat_history=chat_history,\n",
    "                                      dialogue_status=dialogue_status,\n",
    "                                      user_input=user_input,\n",
    "                                      agent_scratchpad=agent_scratchpad)\n",
    "    prompt_output = \"\\nAction:\"+ resultAction + \"\\nObservation:\"+resultObservation\n",
    "\n",
    "    item = {}\n",
    "    item[\"instruction\"] = \"\"\n",
    "    item[\"input\"] = prompt_input\n",
    "    item[\"output\"] = prompt_output\n",
    "    train_data.append(item)\n",
    "\n",
    "import json\n",
    "with open(\"agent_train.json\",mode='w',encoding='utf-8') as fwrite:\n",
    "    json.dump(train_data,fwrite,ensure_ascii=False,indent=4)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "'\\n角色(Role):\\n    你是一个智能发货助手，专门负责处理和提取用户提供的发货信息，确保所有必要的数据都被准确捕获和利用。\\n\\n目标与关键结果(Objectives & Key Results):\\n    目标(Objective): 准确地从用户输入中提取发货信息，并根据这些信息引导用户完成发货流程。\\n\\n关键结果(Key Results):\\n    1. 成功识别并提取用户输入中的车长、收货地、发货地和车型信息。\\n    2. 若用户输入信息不完整，主动询问缺失的细节，如未提及的发货地或收货地。\\n    3. 确保信息准确无误，为顺利完成发货流程提供必要的支持。\\n\\n工具(Actions)：\\n  智能发货助手可以使用的工具如下：\\n    - 发货信息提取:当用户输入文本里面已经提供了一些发货信息，如车长、收货地、发货地和车型时。需要调用这个工具，提取发货信息\\n    - 询问发货信息:当发货助手仍然缺失必要的发货信息时，需要调用这个工具来提示用户输入发货信息。\\n    - 执行发货:当所有必要的发货信息已经被提取和确认后，执行发货流程。\\n    - 取消发货:当用户想要取消发货是，调用这个工具\\n    - 确认发货信息:在执行发货前，和用户确认所有的发货信息是否正确。\\n\\n对话阶段(Status):\\n    - 信息收集阶段：仍然缺失必要的发货信息\\n    - 可执行发货阶段：已经完成所有必要的发货信息的收集\\n\\n步骤(Steps):\\n    1. 智能发货助手必须按照以下格式进行思考和反馈：\\n        - Action: 工具名称应该是[发货信息提取，询问发货信息，执行发货,取消发货,确认发货信息]中的一个。\\n        - Observation: 动作执行以后的反馈。\\n    2. 根据上一步的Observation结果，如果有需要进行下一轮的思考和行动，直到发货流程顺利完成。\\n\\n开始！！\\n历史对话：\\n\\n\\n对话阶段：对话状态-可执行发货阶段\\n用户：你现在帮我发一个 晚上9点~10点装货。@#_#@_装货地，南京众彩物流批发市场一常州凌家塘批发市场，卸货地是无锡朝阳水果批发市场@#_#@_4.2车帮忙装下货。押金150不退还。运费600元，合计750元。\\nAction:发货信息提取\\nObservation:已完成发货信息提取工具调用。请继续思考\\n\\n对话状态:对话状态-可执行发货阶段\\nAction:确认发货信息\\nObservation:客服:好的老板，跟你确认一下。装货地是南京浦口区，目的地是上海青浦华徐公路普洛斯、浦东浦三路领鲜仓库，货物是预制食品，重10吨，体积26方，需要车长6.8米，11月20号13点前装货，11月20日21点前卸货，预算1400元，当天卸货，冷藏温度0-6℃\\n\\n对话状态:对话状态-信息收集阶段\\nAction:发货信息提取\\nObservation:已完成发货信息提取工具调用。请继续思考\\n\\n\\n\\n'"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data[4][\"input\"]"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "'\\nAction:发货信息提取\\nObservation:已完成发货信息提取工具调用。请继续思考'"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "train_data[3][\"output\"]"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "tools_prompt= '''\n",
    "你是用户的智能物流工具选择器，你的任务是根据助手和用户的[对话记录]判断助手解答用户的[当前问题]是否需要使用[工具列表]中的工具，请参考工具的[使用情境]选择工具并抽取对话中可能包含的工具参数，最后以json字符串[{'action': ''}]的格式回答。\n",
    "工具列表：\n",
    "[\n",
    "    {'工具名称': 'lengyun_info_extractor', '使用情境': '用户输入包含如车长、地址、城市名、时间、日期、车型、金额、重量、备注、体积、装货地、目的地、地名、数字、发货时间、卸货时间、发货地点、卸货地点等关键词时，调用该工具。'},\n",
    "    {'工具名称': 'lengyun_topic_back', '使用情境': '用户输入与上文或发货主题无关，进行主题拉回，提示用户回复与上文或发货相关的信息'},\n",
    "    {'工具名称': 'lengyun_info_confirm', '使用情境': '用户输入为确认发货的意图，如“是的，发货吧”，调用该工具'},\n",
    "    {'工具名称': 'lengyun_info_cancel', '使用情境': '用户输入为取消发货的意图，如“算了，我不发货了”，调用该工具'},\n",
    "]\n",
    "'''\n",
    "tools_format = '''\n",
    "对话记录：\n",
    "{history}\n",
    "\n",
    "当前问题：\n",
    "{current}\n",
    "'''\n",
    "\n",
    "agent_zh_2_en = {\n",
    "    \"发货信息提取\":\"lengyun_info_extractor\",\n",
    "    \"执行发货\":\"lengyun_info_confirm\",\n",
    "    \"取消发货\":\"lengyun_info_cancel\"\n",
    "}\n",
    "\n",
    "\n",
    "\n",
    "inputs  = []\n",
    "outputs = []\n",
    "\n",
    "train_data = []\n",
    "for info in datas:\n",
    "    chat_history = info[\"History\"]\n",
    "    user_input = info[\"user_input\"]\n",
    "    resultAction = info[\"agentAction\"]\n",
    "\n",
    "    if resultAction == \"发货信息提取\" and \"#\" in user_input:\n",
    "        continue\n",
    "    if resultAction not in agent_zh_2_en:\n",
    "        continue\n",
    "\n",
    "\n",
    "    converResultAction = agent_zh_2_en[resultAction]\n",
    "\n",
    "    item = {}\n",
    "    item[\"instruction\"] = \"\"\n",
    "    item[\"input\"] = tools_prompt + tools_format.format(history=chat_history,current=user_input)\n",
    "    item[\"output\"] =  \"[{'action': '\"+converResultAction+\"'}]\"\n",
    "    train_data.append(item)\n",
    "\n",
    "    # inputs.append(tools_prompt + tools_format.format(history=chat_history,current=user_input))\n",
    "    # outputs.append( \"[{'action': '\"+converResultAction+\"'}]\")\n",
    "\n",
    "# import pandas as pd\n",
    "# df = pd.DataFrame({\"inputs\":inputs,\"outputs\":outputs})\n",
    "# df.to_excel(\"AgentThink_training_data.xlsx\",index=False)\n",
    "\n",
    "\n",
    "import json\n",
    "with open(\"tools_train.json\",mode='w',encoding='utf-8') as fwrite:\n",
    "    json.dump(train_data,fwrite,ensure_ascii=False,indent=4)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
